package com.stx.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stx.exception.CustomException;
import com.stx.mapper.RuanyueMapper;
import com.stx.pojo.Judge;
import com.stx.pojo.Record;
import com.stx.pojo.Ruanyue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RuanyueService extends ServiceImpl<RuanyueMapper, Ruanyue> {

    @Autowired
    private RuanyueMapper ruanyueMapper;


    public List<Ruanyue> getruanyue() {
        return ruanyueMapper.selectruanyue();
    }


    public void add(Ruanyue ruanyue) {
        //1.用户名一定要有，否则不让新增（后面需要用户名登录）
        if(ruanyue.getRuaName() == null || "".equals(ruanyue.getRuaName())) {
            throw new CustomException("用户名不能为空");
        }
        //2.进行重复性判断，同一名字的管理员不允许重复新增
        //只要根据用户名去数据查询一下就可以
        Ruanyue user = ruanyueMapper.findByName(ruanyue.getRuaName());
        /*if(user != null) {
            //说明已经有了，这里我们就要提示前台不允许新增了
            throw new CustomException("该用户名已存在，请勿重复添加");
        }*/

        if(ruanyue.getRuaDate() == null) {
            ruanyue.setRuaDate(LocalDateTime.now());
        }
        ruanyueMapper.add(ruanyue);
    }

    public void update(Ruanyue ruanyue) {
        ruanyue.setRuaDate(LocalDateTime.now());
        ruanyueMapper.updateID(ruanyue);
    }

    public void delete(Integer id) {
        ruanyueMapper.deleteID(id);
    }

    public Ruanyue last() {
        LambdaQueryWrapper<Ruanyue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ruanyue::getRuaState,1);
        return ruanyueMapper.selectOne(wrapper);
    }

    public Map<Integer, Integer> listRuanyue() {
        List<Ruanyue> ruanyues = ruanyueMapper.selectList(null);
        Map<Integer, Integer> collect = ruanyues.stream().collect(Collectors.toMap(Ruanyue::getId, Ruanyue::getRuaNumber));
        return collect;
    }

    public Page<Ruanyue> find(Object subject, Object address, Integer number, Integer currentPage, Integer pageSize) {
        System.out.println("name=" + subject);
        LambdaQueryWrapper<Ruanyue> wrapper = new LambdaQueryWrapper<>();
        //看plaName是否为空，为空就不执行
        wrapper.like(subject!=null, Ruanyue::getRuaSubject, subject);
        wrapper.like(address!=null, Ruanyue::getRuaAddress, address);
        wrapper.like(number!=null, Ruanyue::getRuaNumber, number);
        //创建分页条件
        Page<Ruanyue> page = new Page<>(currentPage, pageSize);
        //执行分页查询（分页结果）
        Page<Ruanyue> resultPage = ruanyueMapper.selectPage(page, wrapper);
        //当前页数据
        List<Ruanyue> records = resultPage.getRecords();
        //总记录数
        long total = resultPage.getTotal();
        //总页数
        long pages = resultPage.getPages();
        com.stx.common.Page<Ruanyue> pageFind = new com.stx.common.Page<>();
        pageFind.setTotal(total);
        pageFind.setPages(pages);
        return page;
    }

    public int findNumber() {
        LambdaQueryWrapper<Ruanyue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ruanyue::getRuaState, 1);
        Ruanyue ruanyue = ruanyueMapper.selectOne(wrapper);
        return ruanyue.getId();
    }
}
